//
//  WordSort.m
//  ActiveDeck
//
//  Created by Chiao-Wei Liu on 2011/3/3.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WordSort.h"

UIImage *wordImgLeft;
UIImage *wordImgRight;


CGFloat	fontSize;
CGSize selectedSize;
CGSize textSize;
UIColor *FirstC;


@implementation WordSort
@synthesize wID, oPoint, selectWord;


-(id)init:(NSString *)givenWord
   sPoint:(CGPoint)sPoint
{
	self.exclusiveTouch = YES;
	
	oPoint = sPoint;
	self.center = oPoint;
	selectWord = [[NSString alloc] initWithString:givenWord];
	
	fontSize = 60;
	UIFont *myFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:fontSize];
	textSize = [selectWord sizeWithFont:myFont];
	selectedSize = textSize;
	
	fontSize = 20;
	FirstC = [UIColor grayColor];
	//myFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:fontSize];
	
	//
	CGRect frame = CGRectMake(0, 0, textSize.width,textSize.height);
	if(self ==[self initWithFrame:frame])
	{
		[self setNeedsDisplay];
		self.opaque = NO;
	}
	
	return self;
}

-(void) dealloc
{
	[wordImgLeft autorelease];
	[wordImgRight autorelease];
    [super dealloc];
}

#define STRING_INDENT 20

-(void) drawRect:(CGRect)rect
{
	if(wordImgLeft != nil)
	{
		[wordImgLeft drawAtPoint:(CGPointMake(-2, textSize.height/3))];
		[wordImgRight drawAtPoint:(CGPointMake(wordImgLeft.size.width + textSize.width, textSize.height/3))];
	}
	
	CGFloat x = self.bounds.size.width/2 - textSize.width/2 ;
	CGFloat y = self.bounds.size.height/2 - textSize.height/2;
	CGPoint cPoint;
	
	UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:fontSize];
	
	[[UIColor blackColor]set];
	cPoint = CGPointMake(x, y+0.5);
	
	[selectWord drawAtPoint:cPoint forWidth:(self.bounds.size.width -STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
	[FirstC set];
	cPoint = CGPointMake(x, y);
	[selectWord drawAtPoint:cPoint forWidth:(self.bounds.size.width -STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}

- (void)SelectedDisplaySetup
{
	UIImage *lefImage = [UIImage imageNamed:@"WordHighlight_Left.png"];
	UIImage *rigImage = [UIImage imageNamed:@"WordHighlight_Right.png"];
	
	wordImgLeft = lefImage;
	wordImgRight = rigImage;
	
	FirstC = [UIColor whiteColor];
	fontSize = 66;
	UIFont *myFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:fontSize];
	textSize = [selectWord sizeWithFont:myFont];
	
	
	self.frame = CGRectMake(0,0, textSize.width + wordImgLeft.size.width + wordImgRight.size.width, fontSize);
	
	self.center = oPoint;

	
	[self setNeedsDisplay];
}

-(void)ReturnDisplaySetup
{
	wordImgLeft = nil;
	wordImgRight = nil;
	
	FirstC = [UIColor grayColor];
	
	fontSize = 20;
	UIFont *myFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:fontSize];
	textSize = [selectWord sizeWithFont:myFont];
	
	self.frame = CGRectMake(0, 0, textSize.width + 60,textSize.height);
	self.center = oPoint;

	[self setNeedsDisplay];
}

@end
